package org.example.service;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * 消费消息的逻辑
 */
@Service
public class MessageConsumerService {


    /**
     * 监听器模式
     *
     * @param message
     */
//    @RabbitListener(queues = "email.queue")
//    public void psubConsumerEmail(Message message) {
//        byte[] body = message.getBody();
//        String s = new String(body);
//        System.out.println("邮件业务接收消息" + s);
//    }


//    @RabbitListener(bindings = @QueueBinding(value = @Queue("sms.queue"),
//            exchange = @Exchange(value = "pub/sub.exchange", type = "fanout")))
//    public void psubConsumerSMS(Message message) {
//        byte[] body = message.getBody();
//        String s = new String(body);
//        System.out.println("短信业务接收消息" + s);
//    }

    /**
     * routing模式
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("router.error.queue"),
            exchange = @Exchange(value = "routing_exchange", type = "direct"), key = "error_routing_key"))
    public void psubConsumerRouterSMS(Message message) {
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("接收到error等级别日志消息" + s);
    }
    /**
     * routing模式
     * @param message
     */

    @RabbitListener(bindings = @QueueBinding(value =
    @Queue("routing_queue_all"), exchange =
    @Exchange(value = "routing_exchange", type = "direct"),
            key = {"error_routing_key", "info_routing_key", "warning_routing_key"}))
    public void routingConsumerAll(String message) {
        System.out.println("接收到info、error、warning等级别日志消息： " + message);
    }


    /**
     * topic 模式
     * @param message
     */

    @RabbitListener(bindings =@QueueBinding(value =
    @Queue("topic.email.queue"),exchange =
    @Exchange(value = "topic_exchange",type = "topic"),
            key = "info.#.email.#"))
    public void psubConsumerTopic1(Message message) {
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println(" topic email 接收到日志消息" + s);
    }

    /**
     * topic 模式
     * @param message
     */

    @RabbitListener(bindings =@QueueBinding(value =
    @Queue("topic.sms.queue"),exchange =
    @Exchange(value = "topic_exchange",type = "topic"),
            key = "info.#.sms.#"))
    public void psubConsumerTopic2(Message message) {
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println(" topic  _sms 接收到日志消息" + s);
    }
}
